package com.yzfarm.biujoke.biujoke.utils;

import android.util.Log;

import com.yzfarm.biujoke.biujoke.utils.pojo.NewMeinvItem;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hanhy on 2016/9/29.
 */

public class JsoupUtil {

    public static List<NewMeinvItem> getNewMeinvPic(){
        List<NewMeinvItem> list = new ArrayList<NewMeinvItem>();
        try {
            Document doc = Jsoup.connect("http://m.xxxiao.com/").get();
            Elements aElements = doc.select(".thumb-link");
            Log.d("Test","SIZE " + aElements.size());
            Iterator<Element> iterable =aElements.iterator();
            while(iterable.hasNext()){
                NewMeinvItem item = new NewMeinvItem();
                Element aElement = iterable.next();
                String linkHref = aElement.attr("href");
                Elements elements = aElement.select("img[src$=.jpg]");
                Elements subElements = elements.select("[width=620]");
                Element element = subElements.first();
                if(element != null){
                    String pic = element.attr("src");
                    String alt = element.attr("alt");
                    item.alt = alt;
                    item.coverPic = pic;
                    item.linkhref = linkHref;
                    list.add(item);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    public static List<String> getOneMeinvPic(String linkhref){
        List<String> list = new ArrayList<String>();
        try {
            Document doc = Jsoup.connect(linkhref).get();
            Elements aElements = doc.select(".gallery-item");
            Log.d("Test","SIZE " + aElements.size());
            Iterator<Element> iterable =aElements.iterator();
            while(iterable.hasNext()){
                Element aElement = iterable.next();
                Elements links = aElement.select("a[href]");
                Element link = links.first();
                if(link != null){
                    String linkHref = link.attr("href");
                    list.add(linkHref);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }
}
